<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Activation;

use ArrayObject;
use SplObjectStorage;

/**
 * A contributor registry is used to determine what contributors will
 * be notified of an activation session and at which stage of the activation
 * process they will be contributing to
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class ContributorRegistry
{
	/**
	 * @var ArrayObject<string:SplObjectStorage<ContributorInterface>>
	 */
	protected $contributors;
	
	/**
	 * Class constructor
	 * @return void
	 */
	public function __construct()
	{
		$this->contributors = new ArrayObject();
	}
	
	/**
	 * Registers a contributor to a given activation stage
	 *
	 * @param SessionStage $stage
	 * @param ContributorInterface $contributor
	 * @return void
	 */
	public function registerContributor(SessionStage $stage, ContributorInterface $contributor)
	{
		$this->getContributors($stage)->attach($contributor);
	}
	
	/**
	 * Unregisters a contributor from a given activation stage
	 *
	 * @param SessionStage $stage
	 * @param ContributorInterface $contributor
	 * @return void
	 */
	public function unregisterContributor(SessionStage $stage, ContributorInterface $contributor)
	{
		$this->getContributors($stage)->detach($contributor);
	}
	
	/**
	 * Returns the registered contributors for a given activation stage
	 *
	 * @param SessionStage $stage
	 * @return SplObjectStorage<ContributorInterface>
	 */
	public function getContributors(SessionStage $stage)
	{
		$stage = $stage->getType();
		if(!$this->contributors->offsetExists($stage)) {
			$this->contributors->offsetSet($stage, new SplObjectStorage());
		}
		return $this->contributors->offsetGet($stage);
	}
}